#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
#include<thread>
#include<chrono>
#include"util.hpp"

#include<sw/redis++/redis++.h>
using namespace std::chrono_literals;

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::unordered_map;

using sw::redis::Redis;

void test1(Redis &redis){
    std::cout<<"lpush 和 lrange"<<std::endl;
    redis.flushall();

    //插入单个元素
    redis.lpush("key","111");
    //插入一组元素，基于初始化列表
    redis.lpush("key",{"222","333","444"});
    //插入一组元素，基于迭代器
    vector<string> value = {"555","666","777"};
    redis.lpush("key",value.begin(),value.end());
    //lrange获取到列表中的元素
    vector<string> results;
    auto it = std::back_inserter(results);
    redis.lrange("key",0,-1,it);

    printContainer(results);
}
void test2(Redis &redis){
    std::cout<<"rpush"<<std::endl;
    redis.flushall();

    //插入单个元素
    redis.rpush("key","111");
    //插入多个元素，基于初始化列表
    redis.rpush("key",{"222","333","444"});
    //插入多个元素，基于容器
    vector<string> values = {"555","666","777"};
    redis.rpush("key",values.begin(),values.end());
    //使用lrange，获取元素
    vector<string> results;
    auto it = std::back_inserter(results);
    redis.lrange("key",0,-1,it);

    printContainer(results);
}
void test3(Redis &redis){
    std::cout<<"lpop 和 rpop"<<std::endl;
    redis.flushall();
    
    //构造一个list
    redis.rpush("key",{"1","2","3","4"});

    auto result = redis.lpop("key");
    if(result){
        std::cout<<"lpop:"<<result.value()<<std::endl;
    }

    result = redis.rpop("key");
    if(result){
        std::cout<<"rpop:"<<result.value()<<std::endl;
    }
}
void test4(Redis &redis){
    std::cout<<"blpop"<<std::endl;
    redis.flushall();

    auto result = redis.blpop("key");
    if(result){
        std::cout<<"key:"<<result.value().first<<std::endl;
        std::cout<<"elem:"<<result.value().second<<std::endl;
    }else{
        std::cout<<"result无效!"<<std::endl;
    }
}
void test5(Redis &redis){
    std::cout<<"llen"<<std::endl;
    redis.flushall();

    redis.lpush("key",{"111","222","333","444"});
    long long len = redis.llen("key");
    std::cout<<"len:"<<len<<std::endl;
}
int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    //test1(redis);
    //test2(redis);
    //test3(redis);
    //test4(redis);
    test5(redis);
    return 0;
}